package Dao.Impl;

import Dao.RentalRecordDAO;
import MyUtil.JDBCUtils;
import pojo.Customer;
import pojo.RentalRecord;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class RentalRecordDAOImpl implements RentalRecordDAO {
    private Connection rentalRecordDAOImplConn;
    private String sql;
    private PreparedStatement ps;
    private ResultSet rs;

    public RentalRecordDAOImpl() throws Exception {
        rentalRecordDAOImplConn = JDBCUtils.getMyConnection();
    }

    //查询所有的租车信息
    @Override
    public List<RentalRecord> getAllRental() throws SQLException {
        List<RentalRecord> rentalList = new ArrayList<>();

        sql = "select * from rentalrecord";
        ps = rentalRecordDAOImplConn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()){
            RentalRecord rentalRecord = new RentalRecord();
            rentalRecord.setRecord_id(rs.getInt("record_id"));
            rentalRecord.setCustomer_id(rs.getInt("customer_id"));
            rentalRecord.setCar_id(rs.getInt("car_id"));
            rentalRecord.setStart_date(rs.getDate("start_date"));
            rentalRecord.setEnd_date(rs.getDate("end_date"));  // 数据库中存储的是日期类型
            rentalRecord.setRental_fee(rs.getInt("rental_fee"));
            rentalRecord.setRental_status(rs.getString("rental_status"));
            rentalRecord.setRental_days(rs.getInt("rental_days"));
            rentalList.add(rentalRecord);
        }

        return rentalList;
    }

    //根据id修改订单状态
    @Override
    public void updatestatus(int quit_rentalid) throws SQLException {
        String sql = "UPDATE rentalrecord SET rental_status = '已取消' WHERE record_id = ?";
             ps = rentalRecordDAOImplConn.prepareStatement(sql);
            ps.setInt(1, quit_rentalid); // 设置第一个问号
            ps.executeUpdate(); // 执行更新操作
    }

    @Override
    public List<RentalRecord> getRental_ByCarid(int searchText) throws SQLException {
        List<RentalRecord> rentalList = new ArrayList<>();
        sql = "select * from rentalrecord where car_id = ?";
        ps = rentalRecordDAOImplConn.prepareStatement(sql);
        ps.setInt(1, searchText); // 设置第一个问号

        rs = ps.executeQuery();
        while (rs.next()){
            RentalRecord rentalRecord = new RentalRecord();
            rentalRecord.setRecord_id(rs.getInt("record_id"));
            rentalRecord.setCustomer_id(rs.getInt("customer_id"));
            rentalRecord.setCar_id(rs.getInt("car_id"));
            rentalRecord.setStart_date(rs.getDate("start_date"));
            rentalRecord.setEnd_date(rs.getDate("end_date"));  // 数据库中存储的是日期类型
            rentalRecord.setRental_fee(rs.getInt("rental_fee"));
            rentalRecord.setRental_status(rs.getString("rental_status"));
            rentalRecord.setRental_days(rs.getInt("rental_days"));
            rentalList.add(rentalRecord);
        }
        return rentalList;
    }

}
